// pages/detail/detail.js
const app = getApp();
const mod = require('../../utils/util');
const artDetailUrl = require('../../config').artDetail;
const zanUrl = require('../../config').zan;
const collectUrl = require('../../config').collect;
const commentUrl = require('../../config').comment;
var _id = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {

    detail : {}
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;

    _id = options.cid;

    console.log(app.globalData.formid)

    mod.postData(artDetailUrl,{
      openid: wx.getStorageSync('usercode').openid,
      sskey: wx.getStorageSync('usercode').sskey,
      id: _id,
      formid: app.globalData.formid
    },function(res){
      if(res.data.code == 1){
        that.setData({
          detail: res.data.data
        })
      }
      else{
        mod.showToast(res.data.msg)
      }

     
    })
  
  },

  onShow : function(){

    var that = this;

    if (app.globalData.comment != null){

      var _comments = that.data.detail.comments;

      _comments.unshift(app.globalData.comment)

      that.setData({
        ["detail.comments"]: _comments
      })

      app.globalData.comment = null

    }

  
  },

  zan : function(e){

    var that = this;

    var _id = e.currentTarget.dataset.cid;

    mod.postData(zanUrl, {
      openid: wx.getStorageSync('usercode').openid,
      sskey: wx.getStorageSync('usercode').sskey,
      id: _id
    }, function (res) {

      var _zan_num = parseInt(that.data.detail.zan_num);

      

      if (res.data.code == 1) {
        _zan_num++

        that.setData({
          ["detail.zan"]: true,
          ["detail.zan_num"]: _zan_num
        })
        
      }
      mod.showToast(res.data.msg)
    })


  },

  gopage : function(e){
    var _id = e.currentTarget.dataset.cid;
    wx.navigateTo({
      url: '../leave/leave?cid=' + _id+''
    })
  },

  collect : function(e){

    var that = this;
    var _data_collect = e.currentTarget.dataset.collect;

    mod.postData(collectUrl, {
      openid: wx.getStorageSync('usercode').openid,
      sskey: wx.getStorageSync('usercode').sskey,
      id: _data_collect.split(',')[0],
      type: _data_collect.split(',')[1]
    }, function (res) {

      if (res.data.code == 1) {
        
        if(that.data.detail.collect == true){
          that.setData({
            ['detail.collect']: false
          })
        }
        else{
          that.setData({
            ['detail.collect']: true
          })
        }

        
      }
      else{
        mod.showToast(res.data.msg)
      }
      mod.showToast(res.data.msg)
    })

  }
})